package com.example.invoke.web.controller;

import com.example.invoke.model.Img;
import com.example.invoke.service.IImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class ImgController {

    @Qualifier(value = "imgImpl")
    @Autowired
    private IImgService imgService;

    @RequestMapping(value = "/pay")
    public ResponseEntity add(@RequestParam(value = "url") String url) {
        Img img = new Img();
        img.setImgurl(url);
        if (url.contains("http")) {
            img.setDomain(url.split("/")[2]);
        }
        img.setCreateTime(new Date());
        Integer integer = imgService.addImg(img);
        if (integer > 0) {
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.EXPECTATION_FAILED);
    }

    @GetMapping(value = "/x")
    public ResponseEntity c() {
        return ResponseEntity.ok("xxx");
    }

}
